package com.mooc.apigateway.dao;

import com.mooc.apigateway.common.ListResponse;
import com.mooc.apigateway.common.RestResponse;
import com.mooc.apigateway.config.GenericRest;
import com.mooc.apigateway.model.House;
import com.mooc.apigateway.model.HouseQueryReq;
import com.mooc.apigateway.model.HouseUser;
import com.mooc.apigateway.utils.Rests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class HouseDao {

    @Autowired
    private GenericRest rest;
    @Value("${house.service.name}")
    private String houseServiceName;

    public void add(House house) {
        String url = "http://" + houseServiceName + "/house/add";
        ResponseEntity<RestResponse<String>> responseEntity = rest.post(url, house,
                new ParameterizedTypeReference<RestResponse<String>>() {});
        RestResponse<String> response = responseEntity.getBody();
        if(response.getCode() != 0){
            throw new IllegalStateException("Can not add house");
        }
 }

    public ListResponse<House> queryHouse(HouseQueryReq hq) {

        RestResponse<ListResponse<House>> exc = Rests.exc(() -> {
            String url = Rests.toUrl(houseServiceName, "/house/getHouses");
            ResponseEntity<RestResponse<ListResponse<House>>> responseEntity = rest.post(url, hq,
                    new ParameterizedTypeReference<RestResponse<ListResponse<House>>>() {
                    });
            return responseEntity.getBody();
        });

        return exc.getResult();
    }

    public House queryHouseDetail(Long houseId) {

        RestResponse<House> exc = Rests.exc(() -> {
            String url = Rests.toUrl(houseServiceName, "/house/detail?houseId=" + houseId);
            ResponseEntity<RestResponse<House>> responseEntity = rest.get(url,
                    new ParameterizedTypeReference<RestResponse<House>>() {
                    });
            return responseEntity.getBody();

        });
        return exc.getResult();
    }

    public List<House> queryRecommdHouses(Integer size) {

        RestResponse<List<House>> exc = Rests.exc(() -> {
            String url = Rests.toUrl(houseServiceName, "/house/recomHouses?size=" + size);
            ResponseEntity<RestResponse<List<House>>> responseEntity = rest.get(url,
                    new ParameterizedTypeReference<RestResponse<List<House>>>() {
                    });
            return responseEntity.getBody();
   });
        return exc.getResult();



    }

    public Long queryUserIdByHouseId(Long houseId) {

       return Rests.exc(() ->{
            String url = Rests.toUrl(houseServiceName,"/house/queryUserIdByHouseId?id="+houseId);
            ResponseEntity<RestResponse<Long>> responseEntity = rest.get(url, new ParameterizedTypeReference<RestResponse<Long>>() {
            });
            return responseEntity.getBody();
        }).getResult();


    }

    public void bookMark(HouseUser houseUser) {

     Rests.exc(() -> {
            String url = Rests.toUrl(houseServiceName, "/house/bookMark");
            ResponseEntity<RestResponse> responseEntity = rest.post(url, houseUser,
                    new ParameterizedTypeReference<RestResponse>() {
                    });
            return responseEntity.getBody();
        });

    }

    public void unbookMark(HouseUser houseUser) {
        Rests.exc(() -> {
            String url = Rests.toUrl(houseServiceName, "/house/unbookMark");
            ResponseEntity<RestResponse> responseEntity = rest.post(url, houseUser,
                    new ParameterizedTypeReference<RestResponse>() {
                    });
            return responseEntity.getBody();
        });

    }


}
